//
//  HMTabBarItem.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/12.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit

class HMTabBarItem: UITabBarItem {
    
    
    override var badgeValue :String? {
        
        didSet {
            // 通过查看 Xcode 6.4 可以发现一个 _target ,获取打印其类型,就是我们想要的
            let target = self.valueForKey("_target") as! UITabBarController
            
            for subViews in (target.tabBar.subviews) {
                
                if subViews.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                    
                    for svs in subViews.subviews {
                        
                        if svs.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                            
                            for end in svs.subviews {
                                
                                if end.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                    // UnsafeMutablePointer
                                    var i: UInt32 = 0
                                    
                                    // 如何去获取一个未知类身上的属性
                                    let ivars = class_copyIvarList(NSClassFromString("_UIBadgeBackground")!, &i)
                                    for i in 0..<i {
                                        let ivar = ivars[Int(i)]
                                        let name = ivar_getName(ivar)
                                        let nameStr = String(CString: name, encoding: NSUTF8StringEncoding)
                                        
                                        let type = ivar_getTypeEncoding(ivar)
                                        let typeStr = String(CString: type, encoding: NSUTF8StringEncoding)
                                        end .setValue(UIImage(named: "main_badge"), forKey: "_image")
                                        
                                    }
                                }
                            }
                        }
                    }
                }
                
            }
        }
        
        
    }

}
